package com.yuexun.dhdoctor.module.mythings.adapter;

import android.media.MediaPlayer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.mythings.bean.FeedbackBean;
import com.yuexun.dhdoctor.module.mythings.ui.Act_ThingDetail;
import com.yuexun.dhdoctor.module.mythings.ui.Act_ThingDetailMyself;
import com.yuexun.dhdoctor.util.MediaPlayerUtils;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;
import com.yuexun.dhdoctor.widget.ViewByUrlDialog;


import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by work-jx on 2016/7/28.
 */
public class FeedbackAdapter extends KJAdapter<FeedbackBean> {

    TextView voiceTime;
    ImageView voiceFirst;
    ImageView voiceSecend;
    TextView flowTime;
    LinearLayout view;
    FeedbackBean feedbackBean;
    String voiceUrl;
    String viewUrl;

    String downTime;
    int position;

    /**
     * 音频播放的实例
     */
    public static MediaPlayer player = null;
    /**
     * 当前点击的音频的长度
     */
    int voice_time;
    /**
     * 当前点击的音频的长度
     */
    int time;
    /**
     * 当前点击的容器（ImageView）
     */
    private View tempView;
    /**
     * 前后两次点击的是否是一个imageview的标识
     */
    private boolean isSame = false;


    Act_ThingDetail act_thingDetail;
    Act_ThingDetailMyself act_thingDetailMyself;

    ArrayList<FeedbackBean> feedbackBeans;

    public FeedbackAdapter(Act_ThingDetail act_thingDetail, AbsListView view, Collection<FeedbackBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        this.act_thingDetail = act_thingDetail;
        this.feedbackBeans = (ArrayList<FeedbackBean>) mDatas;
    }

    public FeedbackAdapter(Act_ThingDetailMyself act_thingDetailMyself, AbsListView view, Collection<FeedbackBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        this.mDatas = mDatas;
        this.feedbackBeans = (ArrayList<FeedbackBean>) mDatas;
        this.act_thingDetailMyself = act_thingDetailMyself;
    }

    @Override
    public void convert(AdapterHolder adapterHolder, FeedbackBean feedbackBean, boolean b, final int posi) {
        this.feedbackBean = feedbackBean;
        adapterHolder.setText(R.id.tv_comment_person, feedbackBean.getMemberName());
        adapterHolder.setText(R.id.tv_comment_time, feedbackBean.getCreateDate());
        view = adapterHolder.getView(R.id.view);
        voiceTime = adapterHolder.getView(R.id.item_voice_time);
        voiceFirst = adapterHolder.getView(R.id.item_voice_first);
        voiceSecend = adapterHolder.getView(R.id.item_voice_secend);
        flowTime = adapterHolder.getView(R.id.tv_flowTime);
        ImageView writeView = adapterHolder.getView(R.id.write_view);
        LinearLayout voicePlay = adapterHolder.getView(R.id.item_voice_play);
        LinearLayout itemContent = adapterHolder.getView(R.id.item_content);
        LinearLayout ll_extra_file = adapterHolder.getView(R.id.ll_extra_file);
        FixedSwipeListView flv_attachment = adapterHolder.getView(R.id.flv_attachment);


        if (feedbackBeans.get(posi).getAduitStatus() == 1) {
            if (feedbackBeans.get(posi).getFeedbackFileBeans() != null && feedbackBeans.get(posi).getFeedbackFileBeans().size() > 0) {
                ll_extra_file.setVisibility(View.VISIBLE);
                FeedbackFileAdapter adapter = new FeedbackFileAdapter(mCxt, feedbackBeans.get(posi).getFeedbackFileBeans(), false);
                flv_attachment.setAdapter(adapter);
            } else {
                ll_extra_file.setVisibility(View.GONE);
            }
        } else {
            ll_extra_file.setVisibility(View.GONE);
        }


        if (feedbackBean.getFlowTime() == null || feedbackBean.getFlowTime().isEmpty()) {
            flowTime.setVisibility(View.GONE);
            itemContent.setVisibility(View.GONE);
            writeView.setVisibility(View.GONE);
            voicePlay.setVisibility(View.GONE);
        } else {
            flowTime.setVisibility(View.VISIBLE);
            flowTime.setText("用时" + feedbackBean.getFlowTime());
        }

        if (feedbackBean.getAduitStatus() == 0) {
            adapterHolder.setText(R.id.tv_comment, feedbackBean.getContent());
        } else if (feedbackBean.getAduitStatus() == 1) {
            if (feedbackBean.getContentType() == 1) {
                adapterHolder.setText(R.id.tv_comment, "审批意见：同意");
                if (feedbackBean.getContent().isEmpty()) {
                    itemContent.setVisibility(View.GONE);
                } else {
                    itemContent.setVisibility(View.VISIBLE);
                    adapterHolder.setText(R.id.txt_content, feedbackBean.getContent());
                }
                writeView.setVisibility(View.GONE);
                voicePlay.setVisibility(View.GONE);
            } else if (feedbackBean.getContentType() == 2) {
                adapterHolder.setText(R.id.tv_comment, "审批意见：" + feedbackBean.getContent());
                viewUrl = AppConfig.WEB_FILE + feedbackBean.getSignPic();
                ImageLoader.getInstance().displayImage(viewUrl, writeView);
                itemContent.setVisibility(View.GONE);
                writeView.setVisibility(View.VISIBLE);
                voicePlay.setVisibility(View.GONE);
//                writeView.setOnClickListener(this);
            } else {
                adapterHolder.setText(R.id.tv_comment, "审批意见：" + feedbackBean.getContent());
                String temp[] = feedbackBean.getFeedVoice().split("/");
                voiceUrl = AppConfig.WEB_FILE + temp[0];
                voice_time = Integer.valueOf(temp[1]);
                if (feedbackBean.getDownTime() == null || feedbackBean.getDownTime().isEmpty() || feedbackBean.getDownTime().equals("0")) {
                    downTime = temp[1];
                    voiceFirst.setImageResource(R.drawable.voice_wave_first_unselect);
                    voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
                } else {
                    downTime = feedbackBean.getDownTime();
                    if (Integer.valueOf(downTime) % 2 == 0) {
                        voiceFirst.setImageResource(R.drawable.voice_wave_first_select);
                        voiceSecend.setImageResource(R.drawable.voice_wave_secend_select);
                    } else {
                        voiceFirst.setImageResource(R.drawable.voice_wave_first_select);
                        voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
                    }
                }
                voiceTime.setText(downTime + "s");
                itemContent.setVisibility(View.GONE);
                writeView.setVisibility(View.GONE);
                voicePlay.setVisibility(View.VISIBLE);
            }
        } else {
            if (feedbackBean.getContentType() == 1) {
                adapterHolder.setText(R.id.tv_comment, "审批意见：拒绝");
                if (feedbackBean.getContent().isEmpty()) {
                    itemContent.setVisibility(View.GONE);
                } else {
                    itemContent.setVisibility(View.VISIBLE);
                    adapterHolder.setText(R.id.txt_content, feedbackBean.getContent());
                }
                writeView.setVisibility(View.GONE);
                voicePlay.setVisibility(View.GONE);
            } else if (feedbackBean.getContentType() == 2) {
                adapterHolder.setText(R.id.tv_comment, "审批意见：拒绝");
                viewUrl = AppConfig.WEB_FILE + feedbackBean.getSignPic();
                ImageLoader.getInstance().displayImage(viewUrl, writeView);
                itemContent.setVisibility(View.GONE);
                writeView.setVisibility(View.VISIBLE);
                voicePlay.setVisibility(View.GONE);
//                writeView.setOnClickListener(this);
            } else {
                adapterHolder.setText(R.id.tv_comment, "审批意见：拒绝");
                String temp[] = feedbackBean.getFeedVoice().split("/");
                voiceUrl = AppConfig.WEB_FILE + temp[0];
                voice_time = Integer.valueOf(temp[1]);
                if (feedbackBean.getDownTime() == null || feedbackBean.getDownTime().isEmpty()) {
                    downTime = temp[1];
                    voiceFirst.setImageResource(R.drawable.voice_wave_first_unselect);
                    voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
                } else {
                    downTime = feedbackBean.getDownTime();
                    if (Integer.valueOf(downTime) % 2 == 0) {
                        voiceFirst.setImageResource(R.drawable.voice_wave_first_select);
                        voiceSecend.setImageResource(R.drawable.voice_wave_secend_select);
                    } else {
                        voiceFirst.setImageResource(R.drawable.voice_wave_first_select);
                        voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
                    }
                }
                voiceTime.setText(downTime + "s");
                itemContent.setVisibility(View.GONE);
                writeView.setVisibility(View.GONE);
                voicePlay.setVisibility(View.VISIBLE);
            }
        }

        voicePlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                position = posi;
                List<FeedbackBean> datas = (List<FeedbackBean>) mDatas;
                String temp[] = datas.get(position).getFeedVoice().split("/");

                if (view == tempView) {
                    isSame = true;
                } else {
                    tempView = view;
                    isSame = false;
                }
                time = voice_time;

                if (player != null) {
                    if (isSame) {
                        if (player.isPlaying()) {
                            stop();
                        } else {
                            playVoice(AppConfig.WEB_FILE + temp[0]);
                        }
                    } else {
                        stop();
                        playVoice(AppConfig.WEB_FILE + temp[0]);
                    }
                } else {
                    playVoice(AppConfig.WEB_FILE + temp[0]);
                }
            }
        });

        writeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<FeedbackBean> datas = (List<FeedbackBean>) mDatas;
                ViewByUrlDialog dialog = new ViewByUrlDialog(mCxt, AppConfig.WEB_FILE + datas.get(posi).getSignPic());
                dialog.show();
            }
        });
    }

    private void playVoice(String url) {
        MediaPlayerUtils.getInstance(mCxt).playVoice(url, handler);
    }

    public void stop() {
        MediaPlayerUtils.getInstance(mCxt).stopPlay();
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    player = (MediaPlayer) msg.obj;
                    Log.i("player:", (player != null) + "");
                    if (act_thingDetailMyself == null) {
                        act_thingDetail.refresh(player, position);
                    } else {
                        act_thingDetailMyself.refresh(player, position);
                    }
                    break;
                case 2:
                    downTime = (int) msg.obj + "";
                    notifyDataSetChanged();
                    Log.i("time:", downTime + "");
                    break;

                default:
                    break;
            }
        }

        ;
    };

   /* @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.write_view:
                ViewByUrlDialog dialog = new ViewByUrlDialog(mCxt, viewUrl);
                dialog.show();
                break;
        }

    }*/

}
